/**
 * 
 */
$(window).load(function() {
	$("#startdate").datepicker({ minDate: -20, maxDate: "+1M +10D",appendText: "(yyyy-mm-dd)", dateFormat : "yy-mm-dd"});

	
	$("#go").click(function(event) {
		var query = ["key="+$("#research").val(),"date="+$("#startdate").val()];
		search.query(query);
		event.stopImmediatePropagation();
		return false;
	});
	
});


var search = {
	query:function(query) {
		var url = "http://bestcoming.localhost/default/search/searched/";
		
		if(query && query.length>0) {
			for(var i=0;i<query.length;i++) {
				url = url+query[i].replace('=','/')+"/";
			}
			url = url+"format/json";
			$.ajax({
				type:"GET",
				url : url,
				success:function(msg) {
				   var myObjt = jQuery.parseJSON(msg.output);
				   if(myObjt) {
					   $("#searchlist").empty();
					   var title = $("<div>");
					   title.append("Formation");
					   title.addClass("searchitemtitle");
					   $("#searchlist").append(title);
					   for (var i = 0;i<myObjt.length;i++) {
						   var item = new itemResult(myObjt[i]);
						   $("#searchlist").append(item.show());
						}
				   }
				   }
            });
		}
	}	
		
}


var itemResult = function(data) {
	this.title = data.name;
};

itemResult.prototype = {
		title:null,
		detail:null,
		create:function(data) {
			this.title = data.name;
		},
		show : function () {
			var div = $('<div>');
			div.addClass("searchitem");
			div.append("<div>"+this.title+"</div>");
			return div;
		}
		
};
